<?php
class User_AuthController extends Custom_Controller_Action
{
    public function init()
    {   $this->_session	= Zend_Registry::get('mySession');
        $this->_layout = Zend_Layout::getMvcInstance();
        $this->_layout->setLayout('signup');
    }
    
    public function loginAction()
    {   
        $auth = Zend_Auth::getInstance();
        $form = new User_Form_Login();
        $this->view->loginform = $form;
        $user = new User_Model_DbTable_User();
        if($this->getRequest()->isPost())
        {
            $formData = $this->getRequest()->getPost();
            if($form->isValid($formData))
            {
                
                $res = $user->validateLogin($formData);
                if($res == true)
                {   
         
                    $this->RedirectUrl('/');
                }    
                else
                {
                    
                    $this->view->errormsg = _mytranslator('Invalid username/password');
                }    
            }        
            else
            {
                   $this->view->errormsg = _mytranslator('Please login with your username and password');
            }    
        } 
        
    }
  
    public function logoutAction()
    {
       $auth = Zend_Auth::getInstance();
	   $auth->clearIdentity();
	   $this->_redirect('/user/login');
    }
    
    public function forgetpasswordAction()
    {
        $form = new User_Form_Forgetpassword();
        $this->view->forgetpasswordform = $form;
        if($this->getRequest()->isPost())
        {
            $formData = $this->getRequest()->getPost();
            if($form->isValid($formData))
            {
                $email = $formData['user_email'];
                $user = new User_Model_DbTable_User();
                //get user from email
				$userinfo = $user->getByColumn('user_email',$email);
                if($userinfo)
                {    
                    $passwordtoken = Custom_External::generateRandomString(10);
                    $fullname = $userinfo[0]['user_fullname'];
                    //insert the password token in database
                    $data['password_token'] = $passwordtoken;
                    $data['password_token_expiredate']= time()+$this->Config->params->tokenExpire*60*60;
                    $user->updateById($data,$userinfo[0]['id']);
                    
                    $link = $this->_site_url.'/user/auth/resetpassword/token/' . $passwordtoken.'/email/'.$userinfo[0]['user_email'];
                        
                    $lang = Zend_Registry::get('translate');
	                $html = new Zend_View();
                    $html->setScriptPath(APPLICATION_PATH . '/modules/user/views/emails/'.$lang->getLocale().'/');
                    
                    $html->assign('link',$link);
                    $html->assign('fullname', $fullname);
                    $bodyText = $html->render('email.phtml');
                    
                    $mail = new Custom_Mail();
                    $mail->addTo($email);
                    $mail->setSubject($lang->_('Reset password request'));
                    $mail->setBodyHtml($bodyText);
                    if($mail->send())
                    {
                        $this->addMessage('Please check your email for to reset your password');
                    }  
                    else
                    {
                        $this->addMessage('Sorry email is not sent due to technical fault','error');
                        
                    }    
                } 
                else
                {
                    $this->addMessage('Email address is not in our database');
                }
            }        
            
        }        
    }
    
    public function resetpasswordAction()
    {
        $email = $this->getRequest()->getParam('email');
        $token = $this->getRequest()->getParam('token');
        $user = new User_Model_DbTable_User();
        $userinfo = $user->getByColumn('user_email', $email);
        if($userinfo[0]['password_token'] == $token)
        {
           
           
               if($userinfo[0]['password_token_expiredate']-time() >=1 ) 
               {
                   
                   $passwordresetform =  new User_Form_ResetPassword();
                   $this->view->resetform = $passwordresetform;
                   
                   if($this->getRequest()->isPost())
                   {
                        $formdata = $this->getRequest()->getPost();
                        if($passwordresetform->isValid($formdata))
                        {
                            $data['password'] = md5($formdata['password']);
                            $data['password_token'] = '';
                            $data['password_token_expiredate'] = '';
                            $user->updateById($data, $userinfo[0]['id']);
                            $this->addMessage('Password is reset successfully');
                            $this->_redirect('/user/login');
                        }        
                       
                   }        
                   
               }
               else
               {
                   $this->addMessage('Password token is expired');
               }   
        }
        else
        {
            $this->addMessage('Invalid password reset link','error');
			$this->_redirect('/error');
            
        }    
        
    }
    
   
}
?>
